﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDCal.vo;
using GDCal.dao;
using System.Collections;
using System.Globalization;

namespace GDCal
{
    public partial class LandPriceDetailForm : GDBaseForm
    {
        private LandPriceVO landPriceVO;
        private AreaPriceVO areaPriceVO;
        private Boolean forArea;

        public LandPriceDetailForm(LandPriceVO landPriceVO)
        {
            this.forArea = false;
            InitializeComponent();

            //load other information
            ArrayList streetTypes = StreetTypeDao.filter(new StreetTypeVO());
            int i = 0;
            foreach (StreetTypeVO stVO in streetTypes)
            {
                this.streetTypeCombo.Items.Add(stVO.getType());
                if (landPriceVO != null)
                {
                    if (landPriceVO.getStreetTypeId() == stVO.getId())
                        this.streetTypeCombo.SelectedIndex = i;
                }
                i++;
            }
            if (landPriceVO.getId() == 0)
                this.streetTypeCombo.SelectedIndex = 0;

            i = 0;
            foreach (String city in this.getCities())
            {
                this.cityCombo.Items.Add(city);
                if (landPriceVO != null)
                {
                    if (landPriceVO.getCity().Equals(city))
                        this.cityCombo.SelectedIndex = i;
                }
                i++;
            }
            if (landPriceVO.getId() == 0)
                this.cityCombo.SelectedIndex = 0;

            Int32 currentYear = DateTime.Now.Year;
            this.yearUpDown.Minimum = currentYear - 10;
            this.yearUpDown.Maximum = currentYear + 10;
            this.yearUpDown.Value = currentYear;
            
            if (landPriceVO != null)
            {
                this.landPriceVO = landPriceVO;
                this.priceValue.Text = Convert.ToString(this.landPriceVO.getPrice());
                this.noteValue.Text = this.landPriceVO.getNote();
                if (this.landPriceVO.getYear() > 0)
                    this.yearUpDown.Value = this.landPriceVO.getYear();
            }
        }

        public LandPriceDetailForm(AreaPriceVO areaPriceVO)
        {
            this.forArea = true;
            InitializeComponent();
            this.streetTypeLabel.Text = "Loại Vùng";

            //load other information
            ArrayList areaTypes = AreaTypeDao.filter(new AreaTypeVO());
            int i = 0;
            foreach (AreaTypeVO atVO in areaTypes)
            {
                this.streetTypeCombo.Items.Add(atVO.getType());
                if (areaPriceVO != null)
                {
                    if (areaPriceVO.getAreaTypeId() == atVO.getId())
                        this.streetTypeCombo.SelectedIndex = i;
                }
                i++;
            }
            if (areaPriceVO.getId() == 0)
                this.streetTypeCombo.SelectedIndex = 0;

            i = 0;
            foreach (String city in this.getCities())
            {
                this.cityCombo.Items.Add(city);
                if (areaPriceVO != null)
                {
                    if (areaPriceVO.getCity().Equals(city))
                        this.cityCombo.SelectedIndex = i;
                }
                i++;
            }
            if (areaPriceVO.getId() == 0)
                this.cityCombo.SelectedIndex = 0;

            Int32 currentYear = DateTime.Now.Year;
            this.yearUpDown.Minimum = currentYear - 10;
            this.yearUpDown.Maximum = currentYear + 10;
            this.yearUpDown.Value = currentYear;

            if (areaPriceVO != null)
            {
                this.areaPriceVO = areaPriceVO;
                this.priceValue.Text = Convert.ToString(this.areaPriceVO.getPrice());
                this.noteValue.Text = this.areaPriceVO.getNote();
                if (this.areaPriceVO.getYear() > 0)
                    this.yearUpDown.Value = this.areaPriceVO.getYear();
            }
        }

        public LandPriceVO getLandPriceVO()
        {
            return this.landPriceVO;
        }

        public AreaPriceVO getAreaPriceVO()
        {
            return this.areaPriceVO;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(this.priceValue.Text))
            {
                try
                {
                    Double price = Convert.ToDouble(this.priceValue.Text);
                    if (price > 0.0)
                    {
                        String streetType = (String)this.streetTypeCombo.Items[this.streetTypeCombo.SelectedIndex];
                        String city = (String)this.cityCombo.Items[this.cityCombo.SelectedIndex];
                        if (this.forArea)
                        {
                            AreaTypeVO areaTypeVO = AreaTypeDao.getAreaTypeByType(streetType);
                            AreaPriceVO temp = AreaPriceDao.getAreaPriceByUniqueKeySet(areaTypeVO.getId(), Convert.ToInt32(this.yearUpDown.Value), city);
                            if (temp != null && temp.getId() > 0 && temp.getId() != this.areaPriceVO.getId())
                            {
                                MessageBox.Show(GDCalMessages.Notify_AreaPriceDuplicated, GDCalMessages.Notify_Caption);
                            }
                            else
                            {
                                this.areaPriceVO.setAreaTypeId(areaTypeVO.getId());
                                this.areaPriceVO.setYear(Convert.ToInt32(this.yearUpDown.Value));
                                this.areaPriceVO.setPrice(Convert.ToDecimal(this.priceValue.Text));
                                this.areaPriceVO.setNote(this.noteValue.Text);
                                this.areaPriceVO.setCity(city);
                                if (this.areaPriceVO.getId() > 0)
                                {
                                    AreaPriceDao.updateAreaPrice(areaPriceVO);
                                }
                                else
                                {
                                    AreaPriceDao.insertAreaPrice(this.areaPriceVO);
                                    this.areaPriceVO = AreaPriceDao.getAreaPriceByUniqueKeySet(this.areaPriceVO.getAreaTypeId(), this.areaPriceVO.getYear(), city);
                                }
                                this.Close();
                            }
                        }
                        else
                        {
                            StreetTypeVO streetTypeVO = StreetTypeDao.getStreetTypeByType(streetType);
                            LandPriceVO temp = LandPriceDao.getLandPriceByUniqueKeySet(streetTypeVO.getId(), Convert.ToInt32(this.yearUpDown.Value), city);
                            if (temp != null && temp.getId() > 0 && temp.getId() != this.landPriceVO.getId())
                            {
                                MessageBox.Show(GDCalMessages.Notify_LandPriceDuplicated, GDCalMessages.Notify_Caption);
                            }
                            else
                            {
                                this.landPriceVO.setStreetTypeId(streetTypeVO.getId());
                                this.landPriceVO.setYear(Convert.ToInt32(this.yearUpDown.Value));
                                this.landPriceVO.setPrice(Convert.ToDecimal(this.priceValue.Text));
                                this.landPriceVO.setNote(this.noteValue.Text);
                                this.landPriceVO.setCity(city);
                                if (this.landPriceVO.getId() > 0)
                                {
                                    LandPriceDao.updateLandPrice(landPriceVO);
                                }
                                else
                                {
                                    LandPriceDao.insertLandPrice(this.landPriceVO);
                                    this.landPriceVO = LandPriceDao.getLandPriceByUniqueKeySet(this.landPriceVO.getStreetTypeId(), this.landPriceVO.getYear(), city);
                                }
                                this.Close();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(GDCalMessages.Notify_PriceEmpty, GDCalMessages.Notify_Caption);
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(GDCalMessages.Notify_PriceMustBeNumber + "\n" + ex.Message, GDCalMessages.Notify_Caption);
                }
            }
            else
            {
                MessageBox.Show(GDCalMessages.Notify_PriceEmpty, GDCalMessages.Notify_Caption);
            }
        }

        private void priceValue_Leave(object sender, EventArgs e)
        {
            //Remove previous formatting, or the decimal check will fail
            string value = priceValue.Text.Replace(",", "");
            decimal ul;
            //Check we are indeed handling a number
            if (decimal.TryParse(value, out ul))
            {
                priceValue.Text = ul.ToString("N", CultureInfo.CurrentCulture);
            }
            else
            {
                MessageBox.Show(GDCalMessages.Notify_WrongPriceFormat, GDCalMessages.Notify_Caption);
                priceValue.Text = "";
                priceValue.Focus();
            }
        }
    }
}
